<template>
	<body-wrap isFlex>
		<view class="comment">
			<view class="cmList">
				<view class="imgbox">
					<cm-image v-if="detail.userId" :src="detail.userId.avatar"/>
				</view>
				<view class="mainbox" style="border: 0;" v-if="detail.userId">
					<text class="name">{{detail.userId.name || detail.userId.nickname}}</text>
					<text class="time">{{dateFormat('Y-m-d H:i',detail.createTime)}}</text>
					<view class="cmDtBox">{{detail.questionContent}}</view>
					<!-- <view class="zjhf" v-if="detail.replyContent">
						<text class="title">专家回复</text>
						<view class="replayCon">{{detail.replyContent}}</view>
					</view> -->
					<view class="opbox">
						<navigator :url="`/pages/qa/reply?id=${qid}`" class="zjhfbtn">专家回复（{{replytotal}}条）</navigator>
						<div class="numbox">
							<i class="iconfont icon-chakan">{{detail.reads}}</i>
							<i class="iconfont icon-31pinglun">{{detail.comments}}</i>
							<i class="iconfont icon-shoucang">{{detail.collects}}</i>
						</div>
					</view>
				</view>
			</view>
		</view>
		<view class="pltitle">全部评论 {{total}}条</view>
		<view class="comment">
			<view class="cmList" v-for="dt in lists" :key="dt._id">
				<view class="imgbox">
					<cm-image  v-if="dt.userId._id" :src="dt.userId.avatar"/>
				</view>
				<view class="mainbox">
					<text class="name">{{dt.userId.nickname}}</text>
					<text class="time">{{dateFormat('Y-m-d H:i',dt.createTime)}}</text>
					<view class="cmDtBox">{{dt.content}}</view>
				</view>
			</view>
		</view>
		
		<template #footer>
			<view class="fbplbox">
				<input class="myInput" v-model="content" placeholder="发表评论" />
				<view class="sbtn" @click="addCm"><i class="iconfont icon-fabu"></i></view>
			</view>
		</template>
	</body-wrap>
</template>

<script>
	import {defineComponent,reactive,toRefs,onMounted,getCurrentInstance,computed} from 'vue'
	import {onLoad,onShow} from '@dcloudio/uni-app'
	import { useStore } from 'vuex'
	import bodyWrap from "@/components/bodyWrap/index.vue"
	import cmImage from "@/components/cmImage/index.vue"
	import {dateFormat} from "@/utils/common.js"
	import {toast} from "@/utils/common.js"
	import {getQuestionDetail,getCommentList,addComment} from "@/api/consult.js"
	export default defineComponent({
		components:{
			bodyWrap,
			cmImage
		},
		props:{
		
		},
		setup(props,context) {
			const store = useStore()
			const {proxy} = getCurrentInstance()
			
			const state = reactive({
				userInfo:computed(()=>store.state.base.userInfo),
				content:'',
				qid:'',
				detail:{},
				lists:'',
				pageIndex:1,
				pageSize:20,
				total:0,
				replytotal:0,
				//replyLists:[]
			})
			
			onLoad(options=>{
				if(options && options.id){
					state.qid = options.id
				}
				get_question_detail()
				get_comment_list()
				get_teacher_reply()
			})
			
			const get_question_detail = async ()=>{
				const res = await getQuestionDetail(state.qid)
				if(res.code == 200){
					state.detail = res.data
				}
			}
			
			// 获取教师回复
			const get_teacher_reply = async ()=>{
				getCommentList({
					pageIndex:state.pageIndex,
					pageSize:state.pageSize,
					questionId:state.qid,
					type:5 // 教师回复
				}).then(res =>{
					if(res.code == 200){
						console.log("RRRRRRR:",res)
						//state.replyLists = res.data.data
						state.replytotal = res.data.total
					}
				})
			}
			
			// 获取评论
			const get_comment_list = ()=>{
				getCommentList({
					pageIndex:state.pageIndex,
					pageSize:state.pageSize,
					questionId:state.qid,
					type:2 // 对问题的评论
				}).then(res =>{
					if(res.code == 200){
						state.lists = res.data.data
						state.total = res.data.total
					}
				})
			}
			
			onMounted(()=>{
				
			})
			
			const addCm = async ()=>{
				if(!state.content){
					return toast('请输入评论内容')
				}
				const res = await addComment({
					questionId:state.qid,
					userId:state.userInfo._id,
					content:state.content,
					type:2
				})
				if(res.code!==200){
					return toast('保存出差了！')
				}
				state.content = ''
				get_comment_list()
				get_question_detail()
			}
			
			return {
				...toRefs(state),
				dateFormat,
				addCm
			}
		}
	})
</script>

<style lang="scss" scoped>
	.comment{
		padding: 0 20rpx;
		box-sizing: border-box;
		width: 100%;
		background: #fff;
		.cmList{
			display: flex;
			box-sizing: border-box;
			padding: 20rpx 0 0 0;
			.imgbox{
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				overflow: hidden;
				flex-shrink: 0;
			}
			.mainbox{
				flex: 1;
				box-sizing: border-box;
				flex-shrink: 0;
				padding: 0 0 20rpx 10rpx;
				line-height: normal;
				width: 100%;
				border-bottom: 1px solid #f2f2f2;
				.name{
					display: block;
					font-size: 32rpx;
					color: #e3af2f;
				}
				.time{
					display: block;
					font-size: 28rpx;
					color: #999;
					padding: 15rpx 0;
				}
				.cmDtBox{
					font-size: 28rpx;
					line-height: 46rpx;
				}
				.zjhf{
					background: #f7f6f2;
					padding: 15rpx;
					border-radius: 10rpx;
					margin: 10rpx 0 0 0;
					.title{
						display: block;
						font-size: 30rpx;
						font-weight: bold;
						color: #b47a48;
						padding: 0 0 10rpx 0;
					}
					.replayCon{
						font-size: 28rpx;
						line-height: 46rpx;
						color: #b47a48;
					}
				}
				.opbox{
					display: flex;
					align-items: center;
					justify-content: flex-end;
					padding: 15rpx 0;
					.zjhfbtn{
						display: flex;
						align-items: center;
						justify-content: center;
						height: 50rpx;
						padding: 0 20rpx;
						border-radius: 40rpx;
						border: 1px solid rgba(227,175,47,0.6);
						background: rgba(227,175,47,0.5);
						color: #cc0000;
						font-size: 26rpx;
					}
					.numbox{
						display: flex;
						flex: 1;
						align-items: center;
						justify-content: flex-end;
						.iconfont{
							font-size: 26rpx;
							color: #999;
							&+.iconfont{
								margin-left: 20rpx;
							}
							&:before{
								font-size: 30rpx;
								color: #333;
								font-weight: 700;
							}
						}
					}
					
				}
			}
		}
	}
	.pltitle{
		font-size: 30rpx;
		font-weight: 600;
		padding: 20rpx;
	}
	.fbplbox{
		display: flex;
		padding: 20rpx;
		background: #fff;
		.myInput{
			flex: 1;
			height: 80rpx;
			background: #eee;
			padding: 0 15rpx;
			border-top-left-radius: 10rpx;
			border-bottom-left-radius: 10rpx;
			overflow: hidden;
		}
		.sbtn{
			display: flex;
			align-items: center;
			justify-content: center;
			height: 80rpx;
			background: #e6e6e6;
			width: 80rpx;
			border-top-right-radius: 10rpx;
			border-bottom-right-radius: 10rpx;
			overflow: hidden;
			.iconfont{
				font-size: 40rpx;
				color: #000;
			}
		}
	}
	
</style>
